package com.itheima.takeout.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itheima.takeout.MyApplication;
import com.itheima.takeout.R;
import com.itheima.takeout.model.net.bean.GoodsInfo;
import com.itheima.takeout.model.net.bean.GoodsTypeInfo;
import com.itheima.takeout.presenter.fragment.GoodsFragmentPresenter;
import com.itheima.takeout.ui.activity.BusinessActivity;
import com.itheima.takeout.ui.fragment.GoodsFragment;
import com.itheima.takeout.util.Constant;
import com.itheima.takeout.util.CountPriceFormater;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Apple on 2016/9/3.
 */
public class CartListAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<GoodsInfo> goodsInfos;
    private final MyApplication application;

    public void setGoodsInfos(List<GoodsInfo> goodsInfos) {
        this.goodsInfos = goodsInfos;
        notifyDataSetChanged();
    }

    public CartListAdapter(Context context, List<GoodsInfo> goodsInfos) {
        this.context = context;
        this.goodsInfos = goodsInfos;
        application = (MyApplication) ((BusinessActivity)context).getApplication();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_cart, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        GoodsInfo goodsInfo = goodsInfos.get(position);
        ViewHolder viewHolder = (ViewHolder) holder;

        viewHolder.tvName.setText(goodsInfo.getName());
        float allPrice = goodsInfo.getNewPrice() * goodsInfo.getCount();
        viewHolder.tvTypeAllPrice.setText(CountPriceFormater.format(allPrice));

        viewHolder.tvCount.setText(goodsInfo.getCount() + "");

        viewHolder.setPosition(position);
    }

    @Override
    public int getItemCount() {
        return goodsInfos != null ? goodsInfos.size() : 0;
    }

     class ViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.tv_name)
        TextView tvName;
        @InjectView(R.id.tv_type_all_price)
        TextView tvTypeAllPrice;
        @InjectView(R.id.ib_minus)
        ImageButton ibMinus;
        @InjectView(R.id.tv_count)
        TextView tvCount;
        @InjectView(R.id.ib_add)
        ImageButton ibAdd;
        @InjectView(R.id.ll)
        LinearLayout ll;

        ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

        private int position;

        public void setPosition(int position) {
            this.position = position;
        }

        @OnClick({R.id.ib_minus, R.id.ib_add})
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.ib_add:
                    doAddOperator();
                    break;
                case R.id.ib_minus:
                    doMinusOperator();
                    break;
            }
        }


        private void doMinusOperator() {
            GoodsInfo goodsInfo = goodsInfos.get(position);
            goodsInfo.setCount(goodsInfo.getCount()-1);

            if(goodsInfo.getCount() == 0){
                //删除缓存
                application.deleteCacheSelectedInfo(goodsInfo.getId());
            }else{
                //更新缓存
                application.updateCacheSelectedInfo(goodsInfo.getId(),Constant.MINUS);
            }

            //刷新购物车列表界面
            BusinessActivity businessActivity = (BusinessActivity)context;
            List<GoodsInfo> goodsInfos = businessActivity.businessActivityPresenter.getCartList();
            businessActivity.cartAdapter.setGoodsInfos(goodsInfos);

            if(goodsInfos.size() == 0){
                //隐藏购物车
                businessActivity.hideCartList();
            }

            //更新购物车界面
            businessActivity.businessActivityPresenter.updateCartData();

            //更改商品列表数据
            int typeId = goodsInfo.getTypeId();
            GoodsFragmentPresenter goodsFragmentPresenter = ((GoodsFragment) businessActivity.listFragment.get(0)).goodsFragmentPresenter;
            goodsFragmentPresenter.updateSelectedTypeInfo(typeId, Constant.MINUS);
            //刷新商品类型和商品列表界面
            goodsFragmentPresenter.updateTypeAndGoodsListUI();
        }

        private void doAddOperator() {
            GoodsInfo goodsInfo = goodsInfos.get(position);
            goodsInfo.setCount(goodsInfo.getCount()+1);
            //刷新购物车列表
            BusinessActivity businessActivity = (BusinessActivity)context;
            businessActivity.cartAdapter.setGoodsInfos(businessActivity.businessActivityPresenter.getCartList());
            //更改商品列表数据
            int typeId = goodsInfo.getTypeId();
            GoodsFragmentPresenter goodsFragmentPresenter = ((GoodsFragment) businessActivity.listFragment.get(0)).goodsFragmentPresenter;
            goodsFragmentPresenter.updateSelectedTypeInfo(typeId, Constant.ADD);
            //刷新商品类型和商品列表界面
            goodsFragmentPresenter.updateTypeAndGoodsListUI();

            //更新购物车界面
            businessActivity.businessActivityPresenter.updateCartData();

            //更新缓存
            application.updateCacheSelectedInfo(goodsInfo.getId(),Constant.ADD);
        }


    }
}
